package com.doge.calcalbum.db.model

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.github.iielse.imageviewer.adapter.ItemType
import com.github.iielse.imageviewer.core.Photo
import java.io.File


@Entity(tableName = "entry")
data class Entry(
    @PrimaryKey(autoGenerate = true)
    val id: Int = 0,
    val albumId: Int,
    val filePath: String,
    val fileSize: Long,
    val createTime: Long,
    val deleteTime: Long,
    val selected: Boolean? = null,
) : Photo {
    override fun id(): Long = id.toLong()
    override fun itemType(): Int {
        return when {
            isVideoSource -> ItemType.VIDEO
            else -> ItemType.PHOTO
        }
    }

    val isVideoSource: Boolean
        get() {
            val suffixIdx = filePath.lastIndexOf(".")
            if (suffixIdx < 0) return false
            val suffix = filePath.substring(suffixIdx + 1)
            val videoType = arrayOf("mp4","wmv","avi","mpeg","rm","rmvb","flv","3gp","mov","mkv","mod")
            return videoType.contains(suffix)
        }
    fun deleteFile() {
        File(filePath).also { if (it.exists()) it.delete() }
    }
}